package com.lorenzogao.lib_view

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import java.lang.RuntimeException

/**
 *  create by LorenzoGao
 *  email: 2508719070@qq.com
 *  TIME: 2019-11-13 15:25
 *  description:
 */

class RatingBar : View {

    private lateinit var mStartNormalBitmap: Bitmap

    private lateinit var mStartFocusBitmap: Bitmap


    private var mGradeNumber = 5


    private var mCurrentGrade = 0

    // 间隔
    private var mInterval = 20

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {

        val array = context?.obtainStyledAttributes(attrs, R.styleable.RatingBar)

        array?.apply {
            mGradeNumber = getInteger(R.styleable.RatingBar_startNumbre, mGradeNumber)
            val startNormalId = array.getResourceId(R.styleable.RatingBar_startNormal, 0)
            if (startNormalId == 0) {
                throw RuntimeException("请设置属性 startNormal ")
            }

            mStartNormalBitmap = BitmapFactory.decodeResource(resources, startNormalId)

            val startFocusId = array.getResourceId(R.styleable.RatingBar_startFocus, 0)
            if (startFocusId == 0) {
                throw RuntimeException("请设置属性 startFocus ")
            }
            mStartFocusBitmap = BitmapFactory.decodeResource(resources, startFocusId)

            recycle()
        }
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val height = mStartFocusBitmap.height + paddingTop + paddingBottom
        val width = mStartFocusBitmap.width * mGradeNumber + paddingLeft +paddingRight+ (mGradeNumber - 1) * mInterval
        setMeasuredDimension(width, height)


    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {

        when (event?.action) {
            MotionEvent.ACTION_DOWN,
            MotionEvent.ACTION_MOVE -> {
                val moveX = event.x
                // 计算分数
                var currentGrade = (moveX / (mStartFocusBitmap.width+mInterval) + 1).toInt()
                if (currentGrade < 0) {
                    currentGrade = 0
                }
                if (currentGrade > mGradeNumber) {
                    currentGrade = mGradeNumber
                }


                if (mCurrentGrade == currentGrade) {
                    return true
                }
                // 刷新显示
                mCurrentGrade = currentGrade
                invalidate()

            }


        }


        return true


    }

    override fun draw(canvas: Canvas) {
        super.draw(canvas)
        for (i in 0..mGradeNumber) {
            var x = i * (mStartNormalBitmap.width.toFloat()+ mInterval)+paddingLeft
            // 结合第二步骤 触摸的时候 mCurrentGrade这个值是不断的变化的
            if (mCurrentGrade > i) {
                canvas.drawBitmap(mStartFocusBitmap, x, paddingTop.toFloat(), null)
            } else {
                canvas.drawBitmap(mStartNormalBitmap, x, paddingTop.toFloat(), null)
            }

        }


    }
}